from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from artmarket.models import Artwork
import datetime
from django.utils import timezone


class ArtworkTodayStatisticView(APIView):
    def get(self, request):
        # 获取当天开始时间和结束时间
        start_time = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
        end_time = start_time + datetime.timedelta(days=1)

        # 获取每个小时的间隔
        time_intervals = [start_time + datetime.timedelta(hours=i) for i in range(25)]
        response_data = []
        for interval_start, interval_end in zip(time_intervals[:-1], time_intervals[1:]):
            artwork_count = Artwork.objects.filter(
                created_at__lt=interval_end,
                created_at__gte=interval_start,
            ).count()
            response_data.append({
                'time': len(response_data) + 1,
                "interval_start": interval_start.strftime("%Y-%m-%d %H:%M:%S"),
                "interval_end": interval_end.strftime("%Y-%m-%d %H:%M:%S"),
                "artwork_count": artwork_count,
            })
        return Response(data={'data': response_data}, status=status.HTTP_200_OK)
